function   [xpeak,ypeak,radial0,image_size,average_background]=clouds_region(image0,radial,edge0,cloudofnumber,x_initial,y_initial,x_final,y_final,scan_displacement,distance)
%find region of atom clouds
%parameters:
% loadaddress='E:\OneDrive\�׳���\learnfiles\matlab����\calibration optical lattice depth\';  %address of files
% expanded='*.asc';      %expanded-name of files
% %files_if=[initialnumber finalnumber]
% files_if=[1 6];
% %if pictures includes 3 images,mode='3picture',else mode='1picture'
% mode='3picture';
%radial:initial radial of atom cloud by manual
%edge,edge1:difference between atom cloud lied in (0,0) and other coordinates and average_background,used as edge of atom cloud
%cloudofnumber:number of atoms cloud in a picture
%x_initial,y_initial,x_final,y_final:initial value and final value of the center of a circle scaning a picture
%scan_displacement:displacement of adjacent scan 
%return values:
%xpeak,ypeak:center of a circle
%radial0:radial of a circle

%download data from image
% [image0]=load_image(loadaddress,expanded,files_if,mode);
image_size=length(image0);

%Smoothen image
h = ones(15,15) /225;
x_peak=zeros(image_size,1);
y_peak=zeros(image_size,1);
average_background=zeros(image_size,1);

%initial radial of region of cloud
% radial=64;
radial_max=140;
circle_size=12;

%edge of circle
edge=0.07;
% edge0=[edge1 edge2];
circle_value=1000;   %> some value number of a circle
numless=50;

% cloudofnumber=3;
xpeak=cell(1,image_size);
ypeak=cell(1,image_size);
xpeak1=cell(1,image_size);
ypeak1=cell(1,image_size);
radial0=cell(1,image_size);
r0=zeros(cloudofnumber,image_size);

% figsize=size(image0(1).data);
% x0=1:figsize(1);
% y0=1:figsize(2);
% [x,y]=meshgrid(x0,y0);
i=1;
while i<=image_size
    cloud_number=1;
    %-----------------------------------------------------
    %if the picture is bad,procedure skips the picture and executes the next picture
    square=150;
    [im_sub_size,numzeros]=ifbadpicture(image0(i).data,150,750,150,750,square);
    if 3*numzeros>im_sub_size(1)*im_sub_size(2)
        i=i+1;xpeak{1,i}=[];ypeak{1,i}=[];radial0{1,i}=[];
        continue;
    end
    %------------------------------------------------------
    image0(i).data = imfilter(image0(i).data,h);
    average_background(i)=(mean(mean(image0(i).data(:,1:100)))+mean(mean(image0(i).data(:,900:1024))))/2;
    
%     [max1,max_position1]=max(image0(i).data(100:900,100:900));   
%     [max2,max_position2]=max(max1);
%     y_peak(i)=max_position1(max_position2);
%     x_peak(i)=max_position2;
%     
%     %find range of maximum cloud
%     [x_circle,y_circle]=circle(radial,circle_size,x_peak(i),y_peak(i));
%     i0=1;
%     while i0<=circle_size*2
%         difference=image0(i).data(y_circle(i0),x_circle(i0))-average_background(i);
%         if (difference>edge)
%            radial=radial+2;  
%             if radial>=radial_max
%                 radial=radial_max;
%                 break;
%             end
%           [x_circle,y_circle]=circle(radial,circle_size,x_peak(i),y_peak(i));
%             i0=0;
%         end
%         i0=i0+1;
%     end
    
    %picture of atom cloud and its range
%     figure(i)
%     pcolor(x,y,image0(i).data);
%     hold on

    %x_peak and y_peak is the center of a circle
%     rectangle('Position',[x_peak(i)-radial,y_peak(i)-radial,2*radial,2*radial],'Curvature',[1,1],  'EdgeColor',[1 0 0]);
%     hold on
%     shading interp;
%     colorbar
%     colormap
%     xlabel('xpixel');ylabel('ypixel');
    
    %find range of all cloud
   %initial value of scan
    x_peak(i)=x_initial;
    y_peak(i)=y_initial;
    xpeak{1,i}=zeros(cloudofnumber,1);
    ypeak{1,i}=zeros(cloudofnumber,1);
    xpeak1{1,i}=zeros(cloudofnumber,1);
    ypeak1{1,i}=zeros(cloudofnumber,1);
    radial0{1,i}=zeros(cloudofnumber,1);
    while (x_peak(i)<=x_final)
        while (y_peak(i)<=y_final)
             y_peak(i)=y_peak(i)+scan_displacement;
             %find bound of a circle
             [x_circle,y_circle]=circle(radial,circle_size,x_peak(i),y_peak(i));
             
           %scan inner of the circle
           c=y_circle(1);
           different_size1=0;
           for i1=1:circle_size*2/4+1
               y_range1=2*y_peak(i)-y_circle(i1):c;
               y_range2=c:y_circle(i1);
               x_range=2*x_peak(i)-x_circle(i1):x_circle(i1);
               difference2=image0(i).data(y_range1,x_range)-average_background(i);
               difference3=image0(i).data(y_range2,x_range)-average_background(i);
               c=2*y_peak(i)-y_circle(i1);
               difference0=[difference2;difference3];
               difference0=difference0(find(difference0>edge0(1)));
               num=length(find(difference0<0));

               %calibrate elements number
               different_size=size(difference0);
               %amount of >edg1 in a circle
               different_size1=different_size1+different_size;
           end
           
           
           k1=1;
           difference1=zeros(circle_size*2,1);
           %1.there are circle_value points in the circle
           if (different_size1(1)>circle_value)&(num<numless)
              while k1<=circle_size*2
                   difference1(k1)=image0(i).data(y_circle(k1),x_circle(k1))-average_background(i);                 
                   k1=k1+1;                   
              end
              difference1=difference1(find(difference1>edge0(1)));
              different1_size=size(difference1);
              %2.atom cloud inside the circle
              if different1_size(1)<1
                  x_position=x_peak(i)-radial:x_peak(i)+radial;
                  y_position=y_peak(i)-radial:y_peak(i)+radial;
                  difference4=image0(i).data(y_position,x_position)-average_background(i); 
                  [max_circle1,max_positionCy]=max(difference4);   
                  [max_circle2,max_positionCx]=max(max_circle1);
                   ypeak1{1,i}(cloud_number,1)=max_positionCy(max_positionCx)+y_peak(i)-radial-1;
                   xpeak1{1,i}(cloud_number,1)=max_positionCx+x_peak(i)-radial-1;
                   r0(cloud_number,i)=sqrt((xpeak1{1,i}(cloud_number,1)-x_peak(i))^2+(ypeak1{1,i}(cloud_number,1)-y_peak(i))^2);
                   if r0(cloud_number,i)<=radial/distance
                      if cloud_number>1
                         if (xpeak1{1,i}(cloud_number-1,1)==xpeak1{1,i}(cloud_number,1))&(ypeak1{1,i}(cloud_number-1,1)==ypeak1{1,i}(cloud_number,1))
                             break;
                         end
                      end

                            
%                      radial initial of smaller atom cloud
                       radial1=50;
                       [x_circle1,y_circle1]=circle(radial1,circle_size,xpeak1{1,i}(cloud_number,1),ypeak1{1,i}(cloud_number,1));
                       k2=1;
                       while  k2<=circle_size*2
                              difference5=image0(i).data(y_circle1(k2),x_circle1(k2))-average_background(i);
                              if difference5>edge0(2)                                 
                                 radial1=radial1+8;    
                                 if radial1>=radial_max
                                     radial1=radial_max;
                                     break;
                                 end
                                 [x_circle1,y_circle1]=circle(radial1,circle_size,xpeak1{1,i}(cloud_number,1),ypeak1{1,i}(cloud_number,1));
                                  k2=0;
                              end
                               k2=k2+1;
                       end
                         ypeak{1,i}(cloud_number,1)=max_positionCy(max_positionCx)+y_peak(i)-radial-1;
                         xpeak{1,i}(cloud_number,1)=max_positionCx+x_peak(i)-radial-1;
                         radial0{1,i}(cloud_number,1)=radial1;
%                          rectangle('Position',[xpeak{1,i}(cloud_number,1)-radial1,ypeak{1,i}(cloud_number,1)-radial1,2*radial1,2*radial1],'Curvature',[1,1],  'EdgeColor',[1 0 0]);
%                          hold on   
                         cloud_number=cloud_number+1;
                   
                  end               
              end             
           end

        end
        y_peak(i)=y_initial;
        x_peak(i)=x_peak(i)+scan_displacement;
    end
    i=i+1;
end
for i2=1:image_size
    xpeak{1,i2}(diff(xpeak{1,i2})==0)=[];
    ypeak{1,i2}(diff(ypeak{1,i2})==0)=[];
    xpeak{1,i2}=xpeak{1,i2}(:);
    ypeak{1,i2}=ypeak{1,i2}(:);
%     if length(xpeak{1,i2})~=length(radial0{1,i2})
%        xpeak{1,i2}(end,1)=[];
%        ypeak{1,i2}(end,1)=[];
%     end
end

% xpeak(diff(xpeak)==0)=[];
% ypeak(diff(ypeak)==0)=[];
% xpeak(find(xpeak==0))=[];
% ypeak(find(ypeak==0))=[];
% radial0(find(radial0==0))=[];


